package cn.iocoder.yudao.module.of.controller.admin.meetingInfo;

import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.of.controller.admin.meetingInfo.vo.*;
import cn.iocoder.yudao.module.of.dal.dataobject.meetingInfo.MeetingInfoDO;
import cn.iocoder.yudao.module.of.service.meetingInfo.MeetingInfoService;

@Tag(name = "管理后台 - 会议信息")
@RestController
@RequestMapping("/of/meeting-info")
@Validated
public class MeetingInfoController {

    @Resource
    private MeetingInfoService meetingInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建会议信息")
    @PreAuthorize("@ss.hasPermission('of:meeting-info:create')")
    public CommonResult<Long> createMeetingInfo(@Valid @RequestBody MeetingInfoSaveReqVO createReqVO) {
        return success(meetingInfoService.createMeetingInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新会议信息")
    @PreAuthorize("@ss.hasPermission('of:meeting-info:update')")
    public CommonResult<Boolean> updateMeetingInfo(@Valid @RequestBody MeetingInfoSaveReqVO updateReqVO) {
        meetingInfoService.updateMeetingInfo(updateReqVO);
        return success(true);
    }

    @PutMapping("/send/dept")
    @Operation(summary = "分派主责部门")
    @PreAuthorize("@ss.hasPermission('of:meeting-info:update')")
    public CommonResult<Boolean> sendDept(@Valid @RequestBody MeetingInfoSaveReqVO updateReqVO) {
        meetingInfoService.sendDept(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除会议信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('of:meeting-info:delete')")
    public CommonResult<Boolean> deleteMeetingInfo(@RequestParam("id") Long id) {
        meetingInfoService.deleteMeetingInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得会议信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('of:meeting-info:query')")
    public CommonResult<MeetingInfoRespVO> getMeetingInfo(@RequestParam("id") Long id) {
        MeetingInfoRespVO meetingInfo = meetingInfoService.getMeetingInfo(id);
        return success(meetingInfo);
    }

    @PostMapping("/page")
    @Operation(summary = "获得会议信息分页")
    @PreAuthorize("@ss.hasPermission('of:meeting-info:query')")
    public CommonResult<PageResult<MeetingInfoRespVO>> getMeetingInfoPage(@Valid @RequestBody MeetingInfoPageReqVO pageReqVO) {
        PageResult<MeetingInfoRespVO> meetingInfoPage = meetingInfoService.getMeetingInfoPage(pageReqVO);
        return success(meetingInfoPage);
    }

    @PostMapping("/minutePage")
    @Operation(summary = "获得会议信息分页")
    @PreAuthorize("@ss.hasPermission('of:meeting-info:query')")
    public CommonResult<PageResult<MeetingInfoRespVO>> getMeetingMinutePage(@Valid @RequestBody MeetingInfoPageReqVO pageReqVO) {
        PageResult<MeetingInfoRespVO> meetingInfoPage = meetingInfoService.getMeetingMinutePage(pageReqVO);
        return success(meetingInfoPage);
    }

    @PostMapping("/task/page")
    @Operation(summary = "获得会议任务信息分页")
    @PreAuthorize("@ss.hasPermission('of:meeting-task:query')")
    public CommonResult<PageResult<MeetingRespVO>> getMeetingTaskPage(@Valid @RequestBody MeetingInfoPageReqVO pageReqVO) {
        PageResult<MeetingRespVO> pageResult = meetingInfoService.getMeetingTaskPage(pageReqVO);
        return success(pageResult);
    }



    @GetMapping("/export-excel")
    @Operation(summary = "导出会议信息 Excel")
    @PreAuthorize("@ss.hasPermission('of:meeting-info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMeetingInfoExcel(@Valid MeetingInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MeetingInfoRespVO> list = meetingInfoService.getMeetingInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "会议信息.xls", "数据", MeetingInfoRespVO.class,
                list);
    }


    @PostMapping("/stat/base")
    @Operation(summary = "获得会议信息分页")
    public CommonResult<JSONObject> baseStat(@Valid @RequestBody MeetingInfoPageReqVO pageReqVO) {
          JSONObject meetingStat = meetingInfoService.getMeetingBaseStat(pageReqVO);
        return success(meetingStat);
    }

    @PostMapping("/stat/type")
    @Operation(summary = "获得会议信息分页")
    public CommonResult<List<Map<String, Object>>> stat(@Valid @RequestBody MeetingInfoPageReqVO pageReqVO) {
        List<Map<String, Object>> meetingTypeStat = meetingInfoService.getMeetingTypeStat(pageReqVO);
        return success(meetingTypeStat);
    }
}